package com.atguigu.utils;

import com.atguigu.model.system.SysMenu;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenus) {
        List<SysMenu> result = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getParentId().longValue() == 0) {
                result.add(getChildren(sysMenu, sysMenus));
            }
        }
        return result;
    }

    private static SysMenu getChildren(SysMenu sysMenu, List<SysMenu> sysMenus) {
        sysMenu.setChildren(new ArrayList<>());
        for (SysMenu menu : sysMenus) {
            if (sysMenu.getId()==menu.getParentId()){
                sysMenu.getChildren().add(getChildren(menu,sysMenus));
            }
        }
        return sysMenu;

    }
}
